package com.greenline.push.core.pushcenter.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.greenline.push.core.enums.Tasks;
import com.greenline.push.core.pushcenter.MsgForSendManager;
import com.greenline.push.dal.pushcenter.MsgForSendDao;
import com.greenline.push.dal.pushcenter.TaskSyncDAO;
import com.greenline.push.dal.pushcenter.dataobject.MsgForSendDO;
import com.greenline.push.dal.pushcenter.dataobject.TaskSyncDO;
import com.greenline.push.dal.query.MsgForSendQuery;

/**
 * 
 * @Type MsgForSendManagerImpl
 * @Desc MsgForSendManagerImpl crud
 * @author shuai.zuo
 * @date 2014年10月10日
 * @Version V1.0
 */
public class MsgForSendManagerImpl implements MsgForSendManager {
    @Autowired
    private MsgForSendDao msgForSendDao;

    @Autowired
    private TaskSyncDAO taskSyncDao;

    @Override
    public Long insert(MsgForSendDO msgForSendDO) {
        return msgForSendDao.insert(msgForSendDO);
    }

    @Override
    public boolean update(MsgForSendDO msgForSendDO) {
        if (null == msgForSendDO) {
            return false;
        }
        return msgForSendDao.update(msgForSendDO) > 0;
    }

    @Override
    public MsgForSendQuery listByQuery(MsgForSendQuery query) {
        return msgForSendDao.listByQuery(query);
    }

    @Override
    public MsgForSendDO getById(Long id) {
        return msgForSendDao.getById(id);
    }

    @Override
    public boolean delete(Long id) {
        if (null == id) {
            return false;
        }
        return msgForSendDao.deleteById(id) > 0;
    }

    @Override
    public MsgForSendDO getLastedRecord() {
        return msgForSendDao.getLastedRecord();
    }

    @Override
    public List<MsgForSendDO> getRealTimeMsgForSendList(Long maxMsgId, Integer count) {
        return msgForSendDao.getRealTimeMsgForSendList(maxMsgId, count);
    }

    @Override
    public List<MsgForSendDO> getPeriodicMsgForSendList(Long maxMsgId, Integer count) {
        // TODO Auto-generated method stub
        return msgForSendDao.getPeriodicMsgForSendList(maxMsgId, count);
    }

    @Override
    public boolean resetStatus() {

        boolean result = true;
        try {
            TaskSyncDO realtimeTask = taskSyncDao.getTaskSyncByType(Tasks.FETCH_ASYNC_REALTIME.getType());
            TaskSyncDO fixedTimeTask = taskSyncDao.getTaskSyncByType(Tasks.FETCH_ASYNC_FIXEDTIME.getType());
            Long lastRecordId = realtimeTask.getLastRecordId();
            // 将任务意外停掉而没有处理的msg_for_send的标记重置下，使得实时任务和定时任务可以重新取值
            msgForSendDao.resetStatus(lastRecordId);
            // 修改task_sync中last_record_id的值
            realtimeTask.setLastRecordId(0l);
            fixedTimeTask.setLastRecordId(0L);
            taskSyncDao.update(realtimeTask);
            taskSyncDao.update(fixedTimeTask);
        } catch (Exception e) {
            result = false;
        }
        return result;

    }

    @Override
    public List<MsgForSendDO> fetchByTypeAndRecord(Integer type, Long record, Integer total) {
        return msgForSendDao.fetchByTypeAndRecord(type, record, total);
    }
}
